package com.neo.goods.web;

import com.neo.common.domain.Category;
import com.neo.common.domain.Goods;
import com.neo.common.feign.CategoryClient;
import com.neo.goods.mapper.GoodsMapper;
import com.neo.goods.service.GoodsService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Administrator
 * @CreateTime: 2024/3/21 8:34
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/goods")
public class GoodsController {

  @Autowired
  private GoodsService goodsService;

  @Autowired
  private GoodsMapper goodsMapper;

  /**
   * 在goods服务里使用了Category服务的远程调用类categoryClient
   */
  @Autowired
  private CategoryClient categoryClient;

  @GetMapping("add")
  public Object add(String category_id,String goods_name,String shelf_life_quantity,String shelf_life_unit,String ext1,String ext2,String ext3,String is_del){
    Goods goods = new Goods();
    goods.setCategory_id(category_id);
    goods.setGoods_name(goods_name);
    goods.setCreate_time(new Date());
    goods.setManufacture_time(new Date());
    goods.setExt1(ext1);
    goods.setExt2(ext2);
    goods.setExt3(ext3);
    goods.setShelf_life_quantity(shelf_life_quantity);
    goods.setShelf_life_unit(shelf_life_unit);
    goods.setIs_del("0");
    int id = goodsService.add(goods);
    return id;
  }
  @GetMapping("findAll")
  public Object findAll(){
    System.out.println("GoodsController=>findAll()==>");
    return goodsMapper.findAll();
  }

  @GetMapping("del")
  public void delete(int id){
    Goods goods = new Goods();
    goods.setId(id);
    goods.setIs_del("1");
    goodsMapper.update_del(goods);
  }

  @GetMapping("update")
  public void update(String name,int id){
    Goods goods = new Goods();
    goods.setId(id);
    goods.setGoods_name(name);
    goodsMapper.update(goods);
  }
  @GetMapping("findById")
  public Object findById(int id){
    return  goodsMapper.findById(id);
  }

  @GetMapping("findAllForCategory")
  public Object findAllForCategory(){
    System.out.println("进入findAllForCategory方法");
    List<Category> list = new ArrayList<>();
    try{
      /**
       * 在goods服务里使用了Category服务的远程调用类categoryClient
       */
      list =categoryClient.findAll();
    }catch (Exception e){
      System.out.println(e.getMessage());
    }
    return list;
  }

}
